//
//  historyViewController.m
//  EasyGo
//
//  Created by newhuidong on 16/1/30.
//  Copyright © 2016年 HuiDong. All rights reserved.
//

#import "historyViewController.h"
#import "URL.h"
#import "historyTableViewCell.h"
#import "historyDetailTableViewController.h"
#import "AppDelegate.h"
#import "AFNetworking.h"
#import "Model.h"
#import "MJRefresh.h"
@interface historyViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
    UIImageView *navBarHairlineImageView;
}

@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)historyDetailTableViewController* detail;

@property(nonatomic,strong)NSMutableDictionary* dataDic;

@end

@implementation historyViewController

-(void)viewDidLoad{
    self.detail=[[historyDetailTableViewController alloc]init];

    [super viewDidLoad];
    
    self.title=@"历史订单";
    
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    //[self loadData];
    navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];

    [self setupRefresh];
    
}

#pragma 隐藏导航栏下面的一条线
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    navBarHairlineImageView.hidden = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    navBarHairlineImageView.hidden = NO;
}



-(void)setupRefresh{
    
    MJRefreshNormalHeader * header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self refreshData];
        
    }];
    [header setTitle:@"松开刷新" forState:MJRefreshStatePulling];
    [header beginRefreshing];
    self.tableView.mj_header=header;
    
}
-(void)refreshData{
    
    [self loadData];
}

-(void)loadData{
    
    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    
    manager.responseSerializer=[AFJSONResponseSerializer serializer];
    
    NSDictionary * dic=[[NSDictionary alloc]init];
    AppDelegate* tempDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    
    dic=@{@"token":tempDelegate.token};
    
    NSString * urlString=[NSString stringWithFormat:@"%@%@",url1,@"GetLastOrder"];
    
    [manager POST:urlString parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dict=(NSDictionary*)responseObject;
        
        
        self.dataDic =dict[@"Data"];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"ERROR:%@",error);
        
    }];
    
}

-(NSMutableDictionary *)dataDic{
    
    if (!_dataDic) {
        
        _dataDic=[NSMutableDictionary dictionary];
    }
    return _dataDic;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    historyTableViewCell * cell=[historyTableViewCell historyWithTableView:tableView];
    
    cell.orderNumber.text=[NSString stringWithFormat:@"%@",self.dataDic[@"OrderNum"]];
    cell.orderNumber.adjustsFontSizeToFitWidth=YES;
    cell.orderNumber.numberOfLines=0;
    
    cell.time.text=[NSString stringWithFormat:@"%@",self.dataDic[@"EndTime"]];
    cell.time.adjustsFontSizeToFitWidth=YES;
    cell.time.numberOfLines=0;
    cell.workLine.text=[NSString stringWithFormat:@"%@",self.dataDic[@"LineName"]];
    cell.workLine.adjustsFontSizeToFitWidth=YES;
    cell.workLine.numberOfLines=0;

    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 150;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIStoryboard * storyboard =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    historyDetailTableViewController * detailVC=[storyboard instantiateViewControllerWithIdentifier:@"historyDetailTableViewController"];

    [self.navigationController pushViewController:detailVC animated:YES];
    
}

@end
